package com.umeox.watch.calendar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.calendar.wheel.ArrayWheelAdapter;
import com.umeox.watch.calendar.wheel.OnWheelScrollListener;
import com.umeox.watch.calendar.wheel.WheelView;

/**
 * Created by Rambo 2023/12/28
 * Describe:
 */
public class ChooseActivity extends ThemeAppCompatActivity {

    public static final String CHOOSE_EXTRA = "choose_extra";

    private WheelView mWheelView;
    private int mCurrentValue;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_act);
        mCurrentValue = getIntent().getIntExtra(CHOOSE_EXTRA, 1);
        mWheelView = findViewById(R.id.wheelView);
        initWheelView(mCurrentValue);

        findViewById(R.id.iv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("value", mCurrentValue);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }


    public void initWheelView(int currentValue) {
        if (currentValue > 12) { //选择年
            int currentValueIndex = 0;
            String[] yearArr = new String[2051 - 2008];
            for (int i = 0; i < yearArr.length; i++) {
                yearArr[i] = "" + (2008 + i);
                if(currentValue == (2008 + i)){
                    currentValueIndex = i;
                }
            }

            ArrayWheelAdapter arrayAdapter = new ArrayWheelAdapter<>(yearArr);
            mWheelView.setAdapter(arrayAdapter);
            mWheelView.setCyclic(true);
            mWheelView.setVisibleItems(5);
            mWheelView.setCurrentItem(currentValueIndex, false);
            mWheelView.addScrollingListener(new OnWheelScrollListener() {
                @Override
                public void onScrollingStarted(WheelView wheel) {
                }

                @Override
                public void onScrollingFinished(WheelView wheel) {
                    mCurrentValue = Integer.parseInt(yearArr[wheel.getCurrentItem()]);
                }
            });
        } else {
            String[] monthArr = new String[12];
            for (int i = 0; i < monthArr.length; i++) {
                monthArr[i] = "" + (1 + i);
            }

            ArrayWheelAdapter arrayAdapter = new ArrayWheelAdapter<>(monthArr);
            mWheelView.setAdapter(arrayAdapter);
            mWheelView.setCyclic(true);
            mWheelView.setVisibleItems(5);
            mWheelView.setCurrentItem(currentValue - 1, false);
            mWheelView.addScrollingListener(new OnWheelScrollListener() {
                @Override
                public void onScrollingStarted(WheelView wheel) {
                }

                @Override
                public void onScrollingFinished(WheelView wheel) {
                    mCurrentValue = Integer.parseInt(monthArr[wheel.getCurrentItem()]);
                }
            });
        }
    }
}
